﻿var currentPage = 1; // 当前页
var pageSize = 50; // 每页显示多少条
var pageall = 0; // 总页数
var total = 0; // 总记录数        

// 读取列表
$(function() {
    // 1最新盘源 2我的收藏盘 3我的盘源 4笋盘查询
    var typeid = LexunJS.GetQueryString("typeid");
    if (typeid == 2)
    showListType(1);
});

// 切换交易类型
function changeTradeType() {
    var id = LexunJS.GetSelectValue("sTradeType");

    if (id == 2)
        $("#spanTradeTypeUnit").text("元/月");
    else
        $("#spanTradeTypeUnit").text("万元");
}

// typeid 1最新盘源 2我的收藏盘 3我的盘源 4笋盘查询
function typeHousing(typeid) {
    $("#hTypeID").val(typeid);

    if (typeid == 1) {        
        self.parent.addTab(' 最新盘源', "../PM/HousingManager.aspx?typeid=1", 'icon-nav');
    }
    else if (typeid == 2) {
        self.parent.addTab(' 我的收藏盘', "../PM/HousingFavorite.aspx?typeid=2", 'icon-nav');
    }
    else if (typeid == 3) {        
        self.parent.addTab(' 我的盘源', "../PM/HousingManager.aspx?typeid=3", 'icon-nav');
    }
    else if (typeid == 4) {
        self.parent.addTab(' 笋盘查询', "../PM/HousingEveryday.aspx?type=1", 'icon-nav');
    }
    else if (typeid == 5) {        
        self.parent.addTab(' 待过期盘', "../PM/HousingManager.aspx?typeid=5", 'icon-nav');
    }
}

function showListType(currentPage) {
    var typeid = LexunJS.GetQueryString("typeid");   // 1最新盘源 2我的收藏盘 3我的盘源 4笋盘查询


    var roid = "";
    $("input[name='HousingControlSelect1$rbUsageType']").each(function() {
        if ($(this).attr("checked") == true)
            roid = $(this).val(); //rbUsageType1 1住宅 2商铺 3写字楼 4商住 5仓库 6厂房 7地皮 8项目盘 9其他* 10 民房

    })

    switch (roid) {
        case "rbUsageType1":
            roid = "1,4";
            break;
        case "rbUsageType2":
            roid = "3,4";
            break;
        case "rbUsageType3":
            roid = "2";
            break;
        case "rbUsageType4":
            roid = "6";
            break;
        case "rbUsageType5":
            roid = "10";
            break;


    } 
    var city1 = document.getElementById('HousingControlSelect1_sCity2').value; //城区
    var city2 = document.getElementById('HousingControlSelect1_sCity3').value; //片区
    var name = $("#iEstateName").val();             // 楼盘名称
    var room = $("#HousingControlSelect1_sRoom").val();                   // 房
    var hall = $("#HousingControlSelect1_sHall").val();                   // 厅
    var tradetype = $("#HousingControlSelect1_sTradeType").val();         // 交易 1出售 2出租 3租售
    var price1 = $("#iPrice1").val();               // 租售价
    var price2 = $("#iPrice2").val();               // 租售价

    var hasKey = 0;                                 // 0没钥匙 1有
    if ($("#cbHasKey").is(":checked"))
        hasKey = 1;

    var isOnly = 0;                                 // 0非独家 1独家
    if ($("#cbIsOnly").attr('checked'))
        isOnly = 1;

    var hasPhoto = 0;                               // 0无图片 1有图
    if ($("#cbHasPhoto").get(0).checked)
        hasPhoto = 1;

    var pocStatus = 0;                              // 0无红本 1有红本
    if ($("#cbPocStatus").get(0).checked)
        pocStatus = 1;

    var mark2 = 0;                                  // 0无满5年免税 1有满5年免税
    if ($("#cbMark2").get(0).checked)
        mark2 = 2;
    
    // 排序字段
    var orderByName = $("#hOrderByName").val();
    var orderByType = $("#hOrderByType").val();
   
    $("#divPage").html(""); // 清空
    $("#divList").html("<img src=\"../images/loading.gif\" />"); // 清空列表 
    $.ajax({
        type: 'POST',
        url: '../Handler/HandlerHousing.ashx',
        data: 'act=housingtype_list&page=' + currentPage + '&city1=' + city1 + '&city2=' + city2 + '&roid=' + roid + '&pagesize=' + pageSize + '&typeid=' + typeid         
                    + '&status=' + status + '&name=' + name + '&room=' + room + '&hall=' + hall + '&tradetype=' + tradetype
                    + '&price1=' + price1 + '&price2=' + price2 + '&hasKey=' + hasKey + '&isOnly=' + isOnly + '&hasPhoto=' + hasPhoto + '&pocStatus=' + pocStatus + '&mark2=' + mark2
                    + '&orderByName=' + orderByName + '&orderByType=' + orderByType,                    
        timeout: '20000',
        error: function() {
            alert("系统繁忙,请稍候后试！");
        }, success: function(json) {
            listCallBack(json);
        }
    });
}

/*
功能: 将返回的json数据进行排版
json: json数据        
*/
function listCallBack(response) {
    var json = eval("(" + response + ")");
    var record = json.record;

    if (record == null || record.length == 0) {
        $("#divList").html("<font color='red'>&nbsp;系统内暂无您查询的信息!</font>");
        return;
    }

    var page = json.page;

    currentPage = page.currentpage;
    pageall = parseInt(page.pageall);
    total = page.total;
    var typeid = LexunJS.GetQueryString("typeid");   // 1最新盘源 2我的收藏盘 3我的盘源 4笋盘查询               

    var arr = [];
    arr.push("<table id='tableList' border='0' cellpadding='0' cellspacing='1' bgcolor='#a8c7ce'>");
    arr.push("<tr>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("<input type=\"checkbox\" name=\"checkboxAll\" id=\"checkboxAll\"  onclick=\"cbSelectAll()\" />");
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("<a href=\"javascript:searchOrderBy('ename')\" >楼盘</a>");
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("<a href=\"javascript:searchOrderBy('buildname')\" >栋阁</a>");    
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("<a href=\"javascript:searchOrderBy('mark1')\" >楼层</a>");  
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");    
    arr.push("<a href=\"javascript:searchOrderBy('roomno')\" >房间号</a>");  
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("<a href=\"javascript:searchOrderBy('room')\" >户型</a>");  
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("<a href=\"javascript:searchOrderBy('acreage')\" >面积</a>");  
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("<a href=\"javascript:searchOrderBy('leaseprice')\" >租售价</a>"); 
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("<a href=\"javascript:searchOrderBy('unitprice')\" >单价</a>"); 
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("<a href=\"javascript:searchOrderBy('remark')\" >备注</a>"); 
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("<a href=\"javascript:searchOrderBy('fitment')\" >装修</a>"); 
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("<a href=\"javascript:searchOrderBy('[use]')\" >用途</a>"); 
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");    
    arr.push("<a href=\"javascript:searchOrderBy('area')\" >片区</a>"); 
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("<a href=\"javascript:searchOrderBy('markdate')\" >委托日期</a>"); 
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");    
    arr.push("<a href=\"javascript:searchOrderBy('uddate')\" >上次跟进</a>"); 
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("<a href=\"javascript:searchOrderBy('markuser')\" >开盘人</a>"); 
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("<a href=\"javascript:searchOrderBy('markudept')\" >开盘人部门</a>"); 
    arr.push("</td>");    
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("<a href=\"javascript:searchOrderBy('tradetype')\" >交易类型</a>"); 
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("盘源状态");
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");    
    arr.push("<a href=\"javascript:searchOrderBy('')\" >盘源编号</a>"); 
    arr.push("</td>");  
    // 权限判断
    var hCompetencesIsEdit = $("#hCompetencesIsEdit").val();    // 0不允许修改 1允许修改
    if (hCompetencesIsEdit == 1) {
        arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
        arr.push("基本操作");
        arr.push("</td>");
    }
    arr.push("</tr>");

    var id_list = "";   // 保存查询出盘源的ID 如 1211,1212,1213...
    var recordLength = record.length;
    for (var i = 0; i < recordLength; i++) {
        if (typeid != 2)
            id_list += record[i].id;
        else
            id_list += record[i].housingid;
        if (i < recordLength - 1)
            id_list += ",";
        
        if (i % 2 == 0)
            arr.push("<tr id='tr_" + record[i].id + "' bgcolor='#FFFFFF' style='cursor: pointer;' onclick=\"selectedTr('" + record[i].id + "')\" >");
        else
            arr.push("<tr id='tr_" + record[i].id + "' bgcolor='#EEEDED' style='cursor: pointer;' onclick=\"selectedTr('" + record[i].id + "')\" >");

        arr.push("<td height='20' style='text-align: left;' class='STYLE6'>");
        if(typeid != 2)
            arr.push("<input type='checkbox' name='checkbox_" + record[i].id + "' id='" + record[i].id + "' onclick=\"cbSelect(this, " + record[i].id + ")\" />");
        else
            arr.push("<input type='checkbox' name='checkbox_" + record[i].housingid + "' id='" + record[i].housingid + "' onclick=\"cbSelect(this, " + record[i].housingid + ")\" />");

        if (record[i].haskey > 0)
            arr.push("<img src=\"../images/u485.gif\" title='有钥匙' />");
        if (record[i].isonly > 0)
            arr.push("<img src=\"../images/u483.gif\" title='独家盘' />");
        if (record[i].hasphoto > 0)
            arr.push("<img src=\"../images/xtgl.gif\" title='该盘有图片' />");
        if (record[i].ctycont > 0)
            arr.push("<img src=\"../images/u481.gif\" title='被反签' />");
        if (record[i].protected > 0)
            arr.push("<img src=\"../images/062.gif\" title='已封盘' />");
        if (record[i].recommend > 0)
            arr.push("<img src=\"../images/mbi_005.gif\" title='被推荐' />");
        arr.push("</td>");

        arr.push("<td height='20' style='text-align:left;' class='STYLE6'>");
        if (typeid == 2)
            arr.push("<a href='#' onclick=\"openDetail('" + record[i].housingid + "')\">" + record[i].ename + "</a>");
        else
            arr.push("<a href='#' onclick=\"openDetail('" + record[i].id + "')\">" + record[i].ename + "</a>");
        arr.push("</td>");  
        arr.push("<td height='20' style='text-align:left;' class='STYLE6'>");        
        arr.push(record[i].buildname);
        arr.push("</td>");              
        arr.push("<td height='20' class='STYLE6'>");
        arr.push(record[i].mark1);
        arr.push("</td>");
        arr.push("<td height='20' class='STYLE6'>");
        arr.push(record[i].roomno);
        arr.push("</td>");       
        arr.push("<td height='20' class='STYLE6'>");
        arr.push(record[i].room + "-" + record[i].hall + "-" + record[i].restroom + "-" + record[i].balcony);
        arr.push("</td>");
        arr.push("<td height='20' class='STYLE6'>");
        arr.push(parseInt(record[i].acreage));
        arr.push("</td>");        
        arr.push("<td height='20' class='STYLE6'>");
        if (record[i].tradetype == 1)
            arr.push(parseInt(record[i].totalprice) + "万");
        else
            arr.push(parseInt(record[i].leaseprice));           
        arr.push("</td>");
        arr.push("<td height='20' class='STYLE6'>");
        arr.push(parseInt(record[i].unitprice));
        arr.push("</td>");        
        arr.push("<td height='20' class='STYLE6' title='" + record[i].remark + "'>");
        if (record[i].remark.length >= 8)
            arr.push(record[i].remark.substring(0, 8) + "...");
        else
            arr.push(record[i].remark);
        arr.push("</td>");
        arr.push("<td height='20' class='STYLE6'>");
        arr.push(record[i].fitment);
        arr.push("</td>");
        arr.push("<td height='20' class='STYLE6'>");


        var useStr = record[i].use;
        arr.push(LexunJS.GetUsageNameById(useStr));
        arr.push("</td>");
        arr.push("<td height='20' class='STYLE6'>");
        arr.push(record[i].area);
        arr.push("</td>");                
        arr.push("<td height='20' class='STYLE6'>");
        arr.push(record[i].markdate);
        arr.push("</td>"); 
        arr.push("<td height='20' class='STYLE6'>");
        arr.push(record[i].uddate);
        arr.push("</td>");
        arr.push("<td height='20' class='STYLE6'>");
        arr.push(record[i].markuser);
        arr.push("</td>");
        arr.push("<td height='20' class='STYLE6'>");
        arr.push(record[i].markudept);
        arr.push("</td>");
    
        arr.push("<td height='20' class='STYLE6'>");
        if (record[i].tradetype == 1)
            arr.push("出售");
        else if (record[i].tradetype == 2)
            arr.push("出租");
        arr.push("</td>");

        arr.push("<td height='20' class='STYLE6'>");
        if (record[i].status == 1)
            arr.push("当前盘");
        else if (record[i].status == 2)
            arr.push("资料盘");
        else if (record[i].status == 3)
            arr.push("无效盘");

        if (record[i].isvalid == 1)
            arr.push("(待审核)");
       
        arr.push("</td>");

        arr.push("<td height='20' class='STYLE6'>");
        if (typeid == 2)
            arr.push(record[i].housingid);
        else
            arr.push(record[i].id);
        arr.push("</td>");

        // 权限判断                
        if (hCompetencesIsEdit == 1) {
            arr.push("<td height='20'  class='STYLE6'>");
            if (typeid == 2)
                arr.push("<input id=\"Button1\" type=\"button\" value=\"取消收藏\" class=\"button_bak\" onclick=\"return favorite(" + record[i].housingid + ", 0)\" />");
      
            arr.push("</td>");
        }

        arr.push("</tr>");
    }
    arr.push("</table>");
    $("#divList").html(arr.join('')); // 显示列表
    $("#divPage").html(""); // 清空

    if (typeid > 0)
        pageHtml = LexunJS.showPage(currentPage, pageall, "showListType"); // 分页
    $("#divPage").html(pageHtml);

    $("#hID_List").val(id_list);
}

// 响应键盘查询
function responseEnter(e) {
    var key = window.event ? e.keyCode : e.which;
    if (key == 13)
        search();
}

// 条件查询
function search() {
    $("#divList").html(""); // 清空列表
    $("#divPage").html(""); // 清空分页

    showListType(1);
}

// 按字段排序
function searchOrderBy(orderName) {
    $("#divList").html("");     // 清空列表
    $("#divPage").html("");     // 清空分页

    $("#hOrderByName").val(orderName);     // 保存排序字段
    var orderByType = $("#hOrderByType").val();
    if (orderByType == "asc")
        $("#hOrderByType").val("desc");
    else
        $("#hOrderByType").val("asc");

    var typeid = LexunJS.GetQueryString("typeid");
    if (typeid > 0) {
        showListType(1);
    }
    else {
        showList(1);
    }
}

// 打开高级查询框
function iAdvancedSearch_onclick() {
    var obj = document.getElementById("divAdvancedSearch");
    
    $("#divAdvancedSearch").toggle();
}

// 单选
function cbSelect(obj, objvalue) {
    if (obj.checked) {
        LexunJS.addOrCutData(1, "hID", objvalue);

    }
    else {
        LexunJS.addOrCutData(0, "hID", objvalue);
    }
}

// 全选
function cbSelectAll() {
    document.getElementById("hID").value = "";

    var cbAll = document.getElementById("checkboxAll");
    var checkList = document.getElementsByTagName("input");

    if (cbAll.checked) {
        for (var i = 0; i < checkList.length; i++) {
            if (checkList[i].type == "checkbox" && checkList[i].id > 0) {
                checkList[i].checked = true;
                if (parseInt(checkList[i].id) > 0) LexunJS.addOrCutData(1, "hID", checkList[i].id);
            }
        }
    }
    else {
        for (var i = 0; i < checkList.length; i++) {
            if (checkList[i].type == "checkbox" && checkList[i].id > 0) {
                checkList[i].checked = false;
                if (parseInt(checkList[i].id) > 0) LexunJS.addOrCutData(0, "hID", checkList[i].id);
            }
        }
    }
}

// 选中某行
function selectedTr(id) {
    $("tr").removeClass("selected");
    $("#tr_" + id).addClass("selected");
}

// 查看详情
function openDetail(id) {
    $("tr").removeClass("selected");
    $("#tr_" + id).addClass("selected");

    var id_list = $("#hID_List").val();
    var url = "../PM/HousingDetail.aspx?id=" + id + "&id_list=" + escape(id_list);

    var style = "left = 50, screenX = 0, top = 50, screenY = 0, resizable = yes, scrollbars = yes,width=750px, height=600px,Toolbar=no,Location=no,Direction=no,Resizeable=no";

    window.open(url, '', style);
//    self.parent.closeTab('盘源详情');
//    self.parent.addTab('盘源详情', url, 'icon-nav');    
//    //window.open("HousingDetail.aspx?id=" + id + "&id_list=" + escape(id_list), "target", LexunJS.openParams());    
}

// 收藏盘 0取消收藏 1收藏
function favorite(id, isfavorite) {
    var outmsg = "收藏";
    var IDList = document.getElementById("hID").value;

    if (isfavorite == 0) {
        outmsg = "取消收藏";
    } else {
        if (IDList == "") {
            alert("请先选择您要" + outmsg + "的盘源！");
            return false;
        }
    }

    hiConfirm("您确定要" + outmsg + "该盘源，请谨慎操作！", "确认框", function(result) {
        if (result) {
            $.ajax({
                type: 'POST',
                url: '../Handler/HandlerHousing.ashx',
                data: 'act=housing_favorite&idlist=' + IDList + '&id=' + id + '&isfavorite=' + isfavorite,
                timeout: '10000',
                error: function() {
                    alert(outmsg + "失败！");
                }, success: function(outmsg) {
                    alert(outmsg);

                    if (isfavorite == 0)
                        showListType(currentPage);
                    else
                        showList(currentPage);

                    document.getElementById("hID").value = "";
                }
            });
        }
    });

    return false;
}


function DelFavorite() {
    var IDList = document.getElementById("hID").value;
    if (IDList == "") {
        alert("请选择你要取消收藏的房源！");
        return false;
     }
    hiConfirm("您确定要取消收藏该盘源，请谨慎操作！", "确认框", function(result) {
        if (result) {
            $.ajax({
                type: 'POST',
                url: '../Handler/HandlerHousing.ashx',
                data: 'act=DelHousingFavorite&idlist=' + IDList,
                timeout: '10000',
                error: function() {
                    alert("取消收藏失败！");
                }, success: function(outmsg) {
                    alert(outmsg);
                    showListType(currentPage);

                    document.getElementById("hID").value = "";
                }
            });
        }
    });
    return false;
 }
